import java.util.Stack;

public class Sort {
    //插入排序
    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = tmp;
        }

    }

    //希尔
    public static void shellSort(int[] arr) {
        int gap = arr.length / 2;
        while (gap > 1) {
            gap /= 2;
            shell(arr, gap);
        }
    }

    public static void shell(int[] arr, int gap) {
        //优化的插入排序
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for (; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + gap] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + gap] = tmp;
        }

    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    //选择排序
    public static void selectSort1(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                //找后面的最小值
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr, minIndex, i);
        }
    }

    public static void selectSort(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right; i++) {
                if (arr[i] < arr[minIndex]) {
                    minIndex = i;
                }
                if (arr[i] > arr[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(arr, minIndex, left);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(arr, maxIndex, right);
            left++;
            right--;
        }
    }

    //创建大根堆
    public static void creatBigheap(int[] arr) {
        for (int parent = (arr.length - 1 - 1) / 2
             ; parent >= 0; parent--) {
            siftDown(arr, parent, arr.length);
        }
    }

    //向下调整
    public static void siftDown(int[] arr, int parent, int end) {
        int child = 2 * parent + 1;
        while (child < end) {
            if (child + 1 < end && arr[child] < arr[child + 1]) {
                child++;
            }
            if (arr[parent] < arr[child]) {
                swap(arr, parent, child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public static void heapSort(int[] arr) {
        creatBigheap(arr);
        int end = arr.length - 1;
        while (end >= 0) {
            swap(arr, 0, end);
            siftDown(arr, 0, end);
            end--;
        }

    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }


    //Hoare
    public static void quickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private static void quick(int[] arr, int start, int end) {
        if (start >= end) {
            return;
        }
        if (end - start + 1 <= 10) {
            insertRange(arr, start, end);
            return;
        }
        int index = midThreeNum(arr, start, end);
        swap(arr, start, index);
        int par = partition(arr, start, end);
        quick(arr, start, par - 1);
        quick(arr, par + 1, end);
    }

    //优化
    public static void insertRange(int[] arr, int left, int right) {
        for (int i = left + 1; i <= right; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = tmp;
        }

    }

    //Hoare法
    private static int partition1(int[] arr, int left, int right) {
        int tmp = arr[left];
        int i = left;
        while (left < right) {
            //先左后右
            while (left < right && arr[right] >= tmp) {
                right--;
            }
            while (left < right && arr[left] <= tmp) {
                left++;
            }
            swap(arr, left, right);
        }
        //找到中值，交换
        swap(arr, left, i);
        return left;
    }

    //挖坑法
    private static int partition(int[] arr, int left, int right) {
        int tmp = arr[left];
        int i = left;
        while (left < right) {
            //先右后左
            while (left < right && arr[right] >= tmp) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= tmp) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = tmp;
        return left;
    }


    //三数取中
    private static int midThreeNum(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        if (arr[left] < arr[right]) {
            if (arr[mid] < arr[left]) {
                return left;
            } else if (arr[mid] > arr[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (arr[mid] < arr[right]) {
                return right;
            } else if (arr[mid] > arr[left]) {
                return left;
            } else {
                return mid;
            }
        }
    }


    public static void quickSortNot(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = arr.length - 1;
        int par = partition(arr,left,right);
        if (par > left + 1){
            stack.push(left);
            stack.push(par-1);
        }
        if (par < right - 1){
            stack.push(par+1);
            stack.push(right);
        }
        while(!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            par = partition(arr,left,right);
            if (par > left + 1){
                stack.push(left);
                stack.push(par-1);
            }
            if (par < right - 1){
                stack.push(par+1);
                stack.push(right);
            }
        }
    }
    public static void mergeSort(int[] arr) {
        mergeSortFun(arr,0,arr.length-1);
    }
    private static void mergeSortFun(int[] arr,int left,int right) {
        if (left >= right) {
            return;
        }
        int mid = left + (right - left) / 2;
        mergeSortFun(arr,left,mid);
        mergeSortFun(arr,mid+1,right);
        merge(arr,left,right,mid);
    }
    private static void merge(int[] arr,int left,int right,int mid) {
        int[] tmp = new int[right - left + 1];
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2) {

        }
    }
    public static void mergeSortNot(int[] arr) {
        int gap = 1;
        while(gap <= arr.length) {

        }
    }
    public static void countSort(int[] arr) {
        //1.求最大最小值
        int maxval = arr[0];
        int minval = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (maxval < arr[i]){
                maxval = arr[i];
            }
        }
        //2.定义数组
        int[] count = new int[maxval - minval + 1];
        //遍历数组
    }
}
